# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file '使用TreeView控件显示树结构.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2 import QtWidgets, QtCore, QtGui
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")

        self.treeWidget = QTreeWidget(self.centralwidget)
        __qtreewidgetitem = QTreeWidgetItem()
        __qtreewidgetitem.setText(0, u"1");
        self.treeWidget.setHeaderItem(__qtreewidgetitem)
        self.treeWidget.setObjectName(u"treeWidget")
        self.treeWidget.setGeometry(QRect(160, 100, 461, 331))
        # 对树结构进行设置
        self.treeWidget.setColumnCount(2)                   # 设置树结构中的列数
        self.treeWidget.setHeaderLabels(['姓名', '职务'])    # 设置列表标题
        # 设置隔行变色显示树节点
        self.treeWidget.setAlternatingRowColors(True)      # 设置隔行变色

        # 为树控件绑定单击信号（槽函数）
        self.treeWidget.clicked.connect(self.gettreetext)

        root = QTreeWidgetItem(self.treeWidget)             # 创建节点
        root.setText(0, '组织结构')
        # 定义字典，存储树结构中显示的数据
        dict = {'任正非': '华为董事长', '马云': '阿里巴巴创始人', '马化腾': '腾讯CEO', '李彦宏': '百度CEO',
                '董明珠': '格力董事长'}
        for key, value in dict.items():

            child = QTreeWidgetItem(root)    # 在“组织结构”的节点中添加相关子节点
            child.setText(0, key)        # 设置第一列的值
            child.setText(1, value)
            self.treeWidget.addTopLevelItem(root)    # 将创建的树节点（组织结构）添加到树控件中
            self.treeWidget.expandAll()          # 展开所有树节点
            if key == '任正非':
                child.setIcon(0, QtGui.QIcon('华为.jpg'))
            elif key == '马云':
                child.setIcon(0, QtGui.QIcon('阿里巴巴.jpg'))
            elif key == '马化腾':
                child.setIcon(0, QtGui.QIcon('腾讯.jpg'))
            elif key == '李彦宏':
                child.setIcon(0, QtGui.QIcon('百度.jpg.jfif'))
            elif key == '董明珠':
                child.setIcon(0, QtGui.QIcon('格力空调.jpg'))

            child.setCheckState(0, QtCore.Qt.Unchecked)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 26))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u4f7f\u7528TreeView\u63a7\u4ef6\u663e\u793a\u6811\u7ed3\u6784", None))
    # retranslateUi

    # 定义获取选中节点文本内容
    def gettreetext(self, index):
        item = self.treeWidget.currentItem()     # 获取当前选中项

        # 弹窗提示框，显示选中项的文本
        QtWidgets.QMessageBox.information(MainWindow, '提示',
                                          '您选择的是：%s -- %s' % (item.text(0), item.text(1)),
                                          QtWidgets.QMessageBox.Ok)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())



